package com.fy.module.repo.controller;

import com.fy.common.dto.PageDto;
import com.fy.common.vo.ApiRest;
import com.fy.common.vo.PageResult;
import com.fy.module.repo.entity.Repo;
import com.fy.module.repo.service.IRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author yang
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/repo")
@CrossOrigin
@Api(description = "题库管理")
public class RepoController {
    @Autowired
    private IRepoService repoService;

    @GetMapping("list")
    @ApiOperation("查询所有的题库")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public List<Repo> findAll(){
        //TODO :补充条件查询
        return  repoService.list();
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public PageResult<Repo> findpage(@RequestBody PageDto<Repo> pageDto){

        return repoService.findPage(pageDto);
    }

    @PostMapping("save")
    @ApiOperation("添加题库")
    public ApiRest save(@RequestBody Repo repo){
        return repoService.saveRepo(repo);
    }

    @GetMapping("delete/{repoId}")
    @ApiOperation("删除题库")
    @PreAuthorize(("hasAnyRole('ROLE_ADMIN')"))
    public ApiRest deleteById(@PathVariable String repoId){
        boolean b = repoService.removeById(repoId);
        return b?ApiRest.success():ApiRest.failure();
    }

    @PostMapping("deleteBatch")
    @ApiOperation("批量删除题库")
    @PreAuthorize(("hasAnyRole('ROLE_ADMIN')"))
    public ApiRest deleteIds(@RequestBody String[] ids){
        boolean b = repoService.removeBatchByIds(Arrays.asList(ids));
        return b?ApiRest.success():ApiRest.failure();
    }

}
